package gui;
import java.awt.Font;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import javax.swing.DefaultListModel;
import javax.swing.JButton;
import javax.swing.JLabel;
import javax.swing.JList;
import javax.swing.JOptionPane;
import javax.swing.JScrollPane;
import javax.swing.ListSelectionModel;

/**
 * COPYRIGHT (C) 2009. Denny Tran, Omar Jaime, David Deng, Julie Hsieh. All Rights Reserved
 * Class is a ViewPanel containing a list of members who can be removed or added.  
 * @author Denny Tran, Omar Jaime, David Deng, Julie Hsieh 
 * @version 1.01 2009-05-05
 */

public class EditGroup extends ViewPanel
{
	private DefaultListModel listModel;
	private JList list;
	private JScrollPane scroll;

	/**
     * Construct ViewPanel containing a list of members who can be removed or added.
     * @param viewManager the container of this panel
     * @param identification specific identification used to reference this panel
     */
    public EditGroup(ViewManager viewManager, String identification)
    {
        super(viewManager, identification);
        
        JLabel title = new JLabel("Edit Group");
        title.setFont(new Font("Serif", Font.PLAIN, 24));
        title.setSize(150, 30);
        title.setLocation(60, 0);
        this.add(title);
        
        listModel = new DefaultListModel();
		list = new JList(listModel);
		list.setSelectionMode(ListSelectionModel.SINGLE_SELECTION);
		list.setLayoutOrientation(JList.VERTICAL);
		scroll = new JScrollPane(list);
		scroll.setLocation(100, 100);
		scroll.setSize(300, 200);
		//intialize existing group members
		listModel.addElement("David Deng");
		listModel.addElement("Julie Hseih");
		listModel.addElement("Omar Jaime");
		listModel.addElement("Denny Tran");
		this.add(scroll);
		
        //buttons
        JButton addMemberButton = new JButton("Add Member");
        addMemberButton.setSize(150,20);
        addMemberButton.setLocation(100, 350);
        addMemberButton.addActionListener(viewManager.addLink("add member"));
        this.add(addMemberButton);
        
        JButton removeMemberButton = new JButton("Remove Member");
        removeMemberButton.setSize(150,20);
        removeMemberButton.setLocation(300, 350);
        removeMemberButton.addActionListener(new ActionListener()
        {
			public void actionPerformed(ActionEvent arg0)
			{
				if(listModel.getSize() != 0) //make sure list isn't empty
				{
					int index = list.getSelectedIndex();
					listModel.remove(index);
					if (index == listModel.getSize()) ///removed item in last position
			            index--;
					list.setSelectedIndex(index);
					list.ensureIndexIsVisible(index);
				}
			}        	
        });
        this.add(removeMemberButton);
    }
    
    /**
     * Paints graphical aspects of the panel
     *@param g the graphical component used to paint the panel 
     */
    public void paintComponent(Graphics g)
    {
    	super.paintComponent(g);
    	
        Graphics2D g2 = (Graphics2D) g;
        
        g2.drawString("Members", 50, 80);
        
        g2.drawLine(30, 90, 500, 90);
    }
    
    /**
     * Adds the new members name to the JList
     * @param name the new member's name
     */
    public void addMember(String name)
    {
    	listModel.addElement(name);
    	repaint();
    }

    /**
     * Clears all text fields and check boxes
     */
    public void clearView()
    {
        list.clearSelection();
    }
}
